package org.atlantis.grosbeak.cable.interceptor;

import org.atlantis.grosbeak.cable.Orderly;

/**
 * 默认的拦截器链实现类。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
public class InterceptorChainImpl implements InterceptorChain {

	private final Interceptor[] interceptors;
	private int index = 0;
	private Object result = null;

	public InterceptorChainImpl(Interceptor[] interceptors) {
		this.interceptors = interceptors;
	}

	public Object getResult() {
		return this.result;
	}

	public void doInterceptor(Orderly orderly) {
		if (index == interceptors.length) {
			result = orderly.execution();
		} else {
			index++;
			interceptors[index - 1].intercept(orderly, this);
		}
	}
}
